package com.qtenv.datasync.api.tuoanxin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.qtenv.datasync.constant.AppConstants;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TuoAnXinApi
 * @Author CBL
 * @Date 2022/3/2 17:20
 **/
public class TuoAnXinApi {
    private static final String HEADER_APP_ID = "appid";
    private static final String HEADER_TIMESTAMP = "timestamp";
    private static final String HEADER_SIGN = "sign";
    private static final String PARAM_KEY = "body";

    /**
     * 获取系统当前时间戳（带毫秒）
     *
     * @return 系统当前时间戳字符串
     */
    private static String GetTimestamp() {
        return String.valueOf(new Date().getTime());
    }

    /**
     * 获取签名
     *
     * @param body    自然排序的请求参数
     * @param sysTime 系统当前时间戳字符串
     * @return
     */
    private static String GetSignName(String body, String sysTime) {
        String source = AppConstants.TAX_APP_ID + PARAM_KEY + "=" + body.replaceAll(" ", "") + AppConstants.TAX_SECRET + sysTime;
        return SecureUtil.md5(source).toString().toLowerCase();
    }

    /**
     * 获取参数MAP
     *
     * @param paramList
     * @return
     */
    private static Map<String, List<Map<String, Object>>> GetParamMap(List<Map<String, Object>> paramList) {
        Map<String, List<Map<String, Object>>> paramMap = MapUtil.newHashMap();
        paramMap.put(PARAM_KEY, paramList);
        return paramMap;
    }

    /**
     * 发送请求，获取数据
     *
     * @param url       URL
     * @param paramList 参数列表
     * @return
     */
    private static String PostAndGet(String url, List<Map<String, Object>> paramList) {
        try {
            Map<String, List<Map<String, Object>>> paramMap = GetParamMap(paramList);
            String sysTime = GetTimestamp();
            String signName = GetSignName(JSON.toJSONString(paramList), sysTime);
            String body = JSON.toJSONString(paramMap);

            return HttpRequest.post(url)
                    .header(HEADER_APP_ID, AppConstants.TAX_APP_ID)//头信息，多个头信息多次调用此方法即可
                    .header(HEADER_TIMESTAMP, sysTime)//头信息，多个头信息多次调用此方法即可
                    .header(HEADER_SIGN, signName)//头信息，多个头信息多次调用此方法即可
                    .header("ContentType", ContentType.JSON.toString())//头信息，多个头信息多次调用此方法即可
                    .body(body)
                    .timeout(10 * 1000)//超时，毫秒
                    .execute().body();
        } catch (HttpException e) {
            //e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取设备信息
     * 超时时间10秒
     *
     * @return
     */
    public static String GetDeviceInfo() {
        return PostAndGet(AppConstants.TAX_GET_ARCHIVE_URL, CollectionUtil.newArrayList());
    }

    /**
     * 获取设备数据
     * 超时时间10秒
     *
     * @param paramList 参数列表
     * @return
     */
    public static String GetDeviceData(List<Map<String, Object>> paramList) {
        return PostAndGet(AppConstants.TAX_GET_DATA_URL, paramList);
    }

    public static void main(String[] args) {
//        System.out.println(GetDeviceInfo());

        String fids = "1806206261,1912216671,1920028,2106209362,2106209363,2106209364,2106209365,2106209368,2106209369,2106209371";
        String fids2 = "2106209372,2106209373,2106209374,2108211232,2110219859,2206209228,21206093780";
        String[] fidss = fids.split(",");

        List<Map<String, Object>> paramList = new ArrayList<>();

//        for (int i = 0; i < fidss.length; i++) {
        Map<String, Object> paramMap = MapUtil.newHashMap();
//            paramMap.put("fmaddress", fidss[i]);
        paramMap.put("fmaddress", "1908211865");
        paramMap.put("starttime", "2023-12-09 00:00:00");
        paramMap.put("number", 300);
        paramList.add(paramMap);
//        }

//            Map<String, Object> paramMap2 = MapUtil.newHashMap();
//            paramMap2.put("fmaddress", "1806206261");
//            paramMap2.put("starttime", "2023-01-02 00:00:00");
//            paramMap2.put("number", 300);
//            paramList.add(paramMap2);

        System.out.println(paramList);
        System.out.println("数据：" + GetDeviceData(paramList));
//[{number=300, fmaddress=1806206261, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=1912216671, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=1920028, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209362, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209363, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209364, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209365, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209368, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209369, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209371, starttime=2023-01-01 00:00:00}]
//[{number=300, fmaddress=1806206261, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=1912216671, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=1920028, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209362, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209363, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209364, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209365, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209368, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209369, starttime=2023-01-01 00:00:00}, {number=300, fmaddress=2106209371, starttime=2023-01-01 00:00:00}]
//        System.out.println("2022-05-30 00:00:05".substring(0,16));
    }
}
